﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ImageManagement
/// </summary>
public class ImageManagement
{
    #region const declaration
    public const string TABLE_NAME = "Images";
    public const string IMAGEID = "imageID";
    public const string PATHLINK = "pathLink";
    public const string TYPE = "type";
    public const string FOREIGNID = "foreignID";
    #endregion


	public ImageManagement()
	{
	}

    public static bool AddImg(string pathLink, int type, int foreignID)
    {
        string strCon = WebConfigurationManager.ConnectionStrings[Common.CONNECT_STRING].ConnectionString;
        SqlConnection cnn = new SqlConnection(strCon);
        cnn.Open();
   
        string sqlQuery = "INSERT INTO Images Values(@pathLink,@type,@foreignID);SELECT scope_identity()";
        SqlCommand command = new SqlCommand(sqlQuery, cnn);
        command.Parameters.AddWithValue("@pathLink", pathLink);
        command.Parameters.AddWithValue("@type", type);
        command.Parameters.AddWithValue("@foreignID", foreignID);

        int n = int.Parse(command.ExecuteScalar().ToString());

        cnn.Close();

        if (n > 0)
            return true;
        else
            return false;
    }

    public static bool EditImg(string pathLink, int imageID)
    {
        string strCon = WebConfigurationManager.ConnectionStrings[Common.CONNECT_STRING].ConnectionString;
        SqlConnection cnn = new SqlConnection(strCon);
        cnn.Open();

        string sqlQuery = "UPDATE Images Set pathLink =@pathLink WHERE imageID=@imageID";
        SqlCommand command = new SqlCommand(sqlQuery, cnn);
        command.Parameters.AddWithValue("@pathLink", pathLink);
        command.Parameters.AddWithValue("@imageID", imageID);

        int n = command.ExecuteNonQuery();

        cnn.Close();

        if (n == 1)
            return true;
        else
            return false;
    }

    public static bool DeleteImg(int imageID)
    {
        string strCon = WebConfigurationManager.ConnectionStrings[Common.CONNECT_STRING].ConnectionString;
        SqlConnection cnn = new SqlConnection(strCon);
        cnn.Open();

        string sqlQuery = "DELETE Images WHERE imageID=@imageID";
        SqlCommand command = new SqlCommand(sqlQuery, cnn);
        command.Parameters.AddWithValue("@imageID", imageID);

        int n = command.ExecuteNonQuery();

        if (n == 0)
            return false;
        else
            return true;
    }

    public static bool DeleteImg(int type,int foreignID)
    {
        string strCon = WebConfigurationManager.ConnectionStrings[Common.CONNECT_STRING].ConnectionString;
        SqlConnection cnn = new SqlConnection(strCon);
        cnn.Open();

        string sqlQuery = "DELETE Images WHERE foreignID=@foreignID AND type=@type";
        SqlCommand command = new SqlCommand(sqlQuery, cnn);
        command.Parameters.AddWithValue("@foreignID", foreignID);
        command.Parameters.AddWithValue("@type", type);

        int n = command.ExecuteNonQuery();

        if (n == 0)
            return false;
        else
            return true;
    }

    public static DataSet GetAllImg()
    {
        string strCon = WebConfigurationManager.ConnectionStrings[Common.CONNECT_STRING].ConnectionString;
        SqlConnection cnn = new SqlConnection(strCon);

        string sqlQuery = "SELECT TOP 20 * FROM Images";
        SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, cnn);

        DataSet dsAlImg = new DataSet();
        adapter.Fill(dsAlImg, "AllImg");

        return dsAlImg;
    }

    public static DataSet GetImgByForeignID(int foreignID, int type)
    {
        string strCon = WebConfigurationManager.ConnectionStrings[Common.CONNECT_STRING].ConnectionString;
        SqlConnection cnn = new SqlConnection(strCon);

        string sqlQuery = "SELECT * FROM Images WHERE foreignID=" + foreignID +" AND type="+type;
        SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, cnn);

        DataSet dsAlImgByForeignID = new DataSet();
        adapter.Fill(dsAlImgByForeignID, "AllImgByForeignID");

        return dsAlImgByForeignID;
    }
}